package com.front.managementfront.cargoinformation.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.front.managementfront.cargoinformation.entity.HktsCargoInformation;
import com.front.managementfront.cargoinformation.mapper.HktsCargoInformationMapper;
import com.front.managementfront.cargoinformation.service.IHktsCargoInformationService;
import com.front.managementfront.constants.CargoInformationConstance;
import com.front.managementfront.dictionary.entity.HktsDictionaryItem;
import com.front.managementfront.dictionary.mapper.HktsDictionaryItemMapper;
import com.front.managementfront.dictionary.service.IHktsDictionaryItemService;
import com.front.managementfront.fegin.IUserAppServiceClient;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;
import com.hkts.management.common.utils.SnowFlakeUtil;
import com.hkts.management.common.vo.UserBasicInfoVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-06-30
 */
@Service
@Transactional
public class HktsCargoInformationServiceImpl extends ServiceImpl<HktsCargoInformationMapper, HktsCargoInformation> implements IHktsCargoInformationService {


    @Autowired
    private HktsCargoInformationMapper hktsCargoInformationMapper;

    @Autowired
    private HktsDictionaryItemMapper hktsDictionaryItemMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IUserAppServiceClient iUserAppServiceClient;

    @Autowired
    public IHktsDictionaryItemService iHktsDictionaryItemService;

    @Override
    public PageInfo selectPage(Page<HktsCargoInformation> page, HktsCargoInformation hktsCargoInformation) {
        PageHelper.startPage((int) page.getCurrent(), (int) page.getSize());
        QueryWrapper<HktsCargoInformation> query = Wrappers.query();
        QueryWrapper<HktsCargoInformation> entityWrapper = getEntityWrapper(query, hktsCargoInformation);
        List<HktsCargoInformation> hktsCargoInformations = hktsCargoInformationMapper.CargoInformationPage();
        PageInfo pageInfo = new PageInfo<>(hktsCargoInformations);
        return pageInfo;
    }


    @Override
    public List<HktsCargoInformation> selectList(HktsCargoInformation hktsCargoInformation) {
        QueryWrapper<HktsCargoInformation> query = Wrappers.query();
        QueryWrapper<HktsCargoInformation> entityWrapper = getEntityWrapper(query, hktsCargoInformation);
        return hktsCargoInformationMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HktsCargoInformation> getEntityWrapper(QueryWrapper<HktsCargoInformation> entityWrapper, HktsCargoInformation hktsCargoInformation) {
        //条件拼接
        if (StringUtils.isNotBlank(hktsCargoInformation.getCreateBy())) {
            entityWrapper.like(HktsCargoInformation.CREATE_BY, hktsCargoInformation.getCreateBy());
        }
        if (StringUtils.isNotBlank(hktsCargoInformation.getUpdateBy())) {
            entityWrapper.like(HktsCargoInformation.UPDATE_BY, hktsCargoInformation.getUpdateBy());
        }
        if (StringUtils.isNotBlank(hktsCargoInformation.getCargoName())) {
            entityWrapper.like(HktsCargoInformation.CARGO_NAME, hktsCargoInformation.getCargoName());
        }
        if (StringUtils.isNotBlank(hktsCargoInformation.getCargoType())) {
            entityWrapper.eq(HktsCargoInformation.CARGO_TYPE, hktsCargoInformation.getCargoType());
        }
        if (StringUtils.isNotBlank(hktsCargoInformation.getIsHotCategories())) {
            entityWrapper.eq(HktsCargoInformation.IS_HOT_CATEGORIES, hktsCargoInformation.getIsHotCategories());
        }
        return entityWrapper;
    }

    @Override
    public List<HktsCargoInformation> selectListByName(HktsCargoInformation hktsCargoInformation) {
        QueryWrapper<HktsCargoInformation> query = Wrappers.query();
        hktsCargoInformation.setIsEnable("1");
        QueryWrapper<HktsCargoInformation> entityWrapper = getEntityWrapper(query, hktsCargoInformation);
        List<HktsCargoInformation> hktsCargoInformations = hktsCargoInformationMapper.selectList(entityWrapper);

        if (hktsCargoInformations.size()>0){
            List<HktsCargoInformation> collect = hktsCargoInformations.stream().map(t -> getbyCargoInformationDataT(t)).collect(Collectors.toList());
            for(int i=0;i<collect.size();i++){
                if(collect.get(i).getCargoName().equals(hktsCargoInformation.getCargoName())){
                    return collect;
                }
            }
            hktsCargoInformations.add(hktsCargoInformation);
            return collect;
        }
        HktsCargoInformation hktsCargoInformation1 = new HktsCargoInformation();
        hktsCargoInformation1.setCargoName(hktsCargoInformation.getCargoName());
        hktsCargoInformation1.setCargoType("40");
        hktsCargoInformations.add(hktsCargoInformation1);
        List<HktsCargoInformation> collect = hktsCargoInformations.stream().map(t -> getbyCargoInformationDataT(t)).collect(Collectors.toList());
        /*hktsCargoInformations.add(hktsCargoInformation);*/
        return collect;
    }


    public HktsCargoInformation getbyCargoInformationData(HktsCargoInformation hktsCargoInformation) {
        HktsDictionaryItem hktsDictionaryItem = hktsDictionaryItemMapper.selectById(hktsCargoInformation.getCargoType());
        hktsCargoInformation.setCargoTypeName(hktsDictionaryItem.getItemValue());
        return hktsCargoInformation;
    }

    public HktsCargoInformation getbyCargoInformationDataT(HktsCargoInformation hktsCargoInformation) {
        HktsDictionaryItem hktsDictionaryItem = hktsDictionaryItemMapper.selectById(hktsCargoInformation.getCargoType());
        hktsCargoInformation.setCargoTypeName(hktsDictionaryItem.getItemValue());
        return hktsCargoInformation;
    }

    @Override
    public void searchCargoSave(HktsCargoInformation hktsCargoInformation) {
        Result<UserBasicInfoVo> userBasicInfoVoResult = iUserAppServiceClient.remoteUserBasicInfo();
        Object o = redisTemplate.opsForValue().get(CargoInformationConstance.HISTORY_SEARCH + userBasicInfoVoResult.getResult().getId());
        Gson gson = new Gson();
        List<Map> list = new ArrayList<Map>();
        Map map = new HashMap();
        map.put("cargoName", hktsCargoInformation.getCargoName());
        map.put("cargoTypeName", hktsCargoInformation.getCargoTypeName());
        map.put("cargoType",hktsCargoInformation.getCargoType());
        if (o == null) {
            list.add(map);
            String json = gson.toJson(list);
            redisTemplate.opsForValue().set(CargoInformationConstance.HISTORY_SEARCH + userBasicInfoVoResult.getResult().getId(), json);
        } else {
            String s = redisTemplate.opsForValue().get(CargoInformationConstance.HISTORY_SEARCH + userBasicInfoVoResult.getResult().getId()).toString();
            list = gson.fromJson(s, List.class);
            Iterator iterator = list.iterator();
            while (iterator.hasNext()) {
                Map<String, Object> next = (Map<String, Object>) iterator.next();
                if (next.get("cargoName").toString().equals(hktsCargoInformation.getCargoName())) {
                    iterator.remove();
                }
            }
            list.add(map);
            String s1 = gson.toJson(list);
            redisTemplate.opsForValue().set(CargoInformationConstance.HISTORY_SEARCH + userBasicInfoVoResult.getResult().getId(), s1);
        }
        //将类别保存
        //需要在修改类别时将货物类别数据同步
        redisTemplate.opsForValue().set(CargoInformationConstance.CARGO_TYPE + hktsCargoInformation.getCargoName(), hktsCargoInformation.getCargoTypeName()
        );
    }

    @Override
    public void saveGargoInformation(String cargoName) {
        QueryWrapper<HktsCargoInformation> query = Wrappers.query();
        query.lambda().eq(HktsCargoInformation::getCargoName, cargoName);
        HktsCargoInformation hktsCargoInformation = hktsCargoInformationMapper.selectOne(query);
        if (hktsCargoInformation == null) {
            hktsCargoInformation = new HktsCargoInformation();
            hktsCargoInformation.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
            hktsCargoInformation.setDelFlag(0);
            hktsCargoInformation.setCargoType("40");
            hktsCargoInformation.setUpdateTime(LocalDateTime.now());
            hktsCargoInformation.setCreateTime(LocalDateTime.now());
            hktsCargoInformation.setIsEnable("1");
            hktsCargoInformation.setCargoName(cargoName);
            hktsCargoInformationMapper.insert(hktsCargoInformation);
        }


    }

    @Override
    public List<HktsCargoInformation> getHotCategories(){
        QueryWrapper<HktsCargoInformation> query = Wrappers.query();
        HktsCargoInformation hktsCargoInformation =new HktsCargoInformation();
        hktsCargoInformation.setIsHotCategories("1");
        QueryWrapper<HktsCargoInformation> entityWrapper = getEntityWrapper(query, hktsCargoInformation);
        List<HktsCargoInformation> hktsCargoInformations = hktsCargoInformationMapper.selectList(entityWrapper);
        List<HktsCargoInformation> collect = hktsCargoInformations.stream().map(t -> getbyCargoInformationDataT(t)).collect(Collectors.toList());
        return collect;
    }
    @Override
    public List<HktsCargoInformation> getCargoInformation(){
        QueryWrapper<HktsCargoInformation> query = Wrappers.query();
        HktsCargoInformation hktsCargoInformation =new HktsCargoInformation();
        QueryWrapper<HktsCargoInformation> entityWrapper = getEntityWrapper(query, hktsCargoInformation);
        List<HktsCargoInformation> hktsCargoInformations = hktsCargoInformationMapper.selectList(entityWrapper);
        List<HktsCargoInformation> collect = hktsCargoInformations.stream().map(t -> getbyCargoInformationDataT(t)).collect(Collectors.toList());
        return collect;
    }

    @Override
    public boolean checkIsRepeat(HktsCargoInformation hktsCargoInformation) {
        QueryWrapper<HktsCargoInformation> query = Wrappers.query();
        query.lambda().eq(HktsCargoInformation::getCargoName,hktsCargoInformation.getCargoName());
        query.lambda().ne(StringUtils.isNotBlank(hktsCargoInformation.getId()),HktsCargoInformation::getId,hktsCargoInformation.getId());
        List<HktsCargoInformation> hktsCargoInformations = hktsCargoInformationMapper.selectList(query);
        if(hktsCargoInformations.size()==0){
            return false;
        }
        return true;
    }

    @Override
    public List<HktsCargoInformation> getCargoInformationByCargoType(String cargoType) {
        QueryWrapper<HktsCargoInformation> query = Wrappers.query();
        query.lambda().eq(HktsCargoInformation::getCargoType,cargoType);

        List<HktsCargoInformation> hktsCargoInformations = hktsCargoInformationMapper.selectList(query);
        List<HktsCargoInformation> collect = hktsCargoInformations.stream().map(t -> getbyCargoInformationDataT(t)).collect(Collectors.toList());
        return collect;
    }
    @Override
    public Result<Object> getCargoInformationByCargoName(String cargoName) {
        QueryWrapper<HktsCargoInformation> query = Wrappers.query();
        query.lambda().eq(HktsCargoInformation::getCargoName,cargoName);
        HktsCargoInformation hktsCargoInformation = hktsCargoInformationMapper.selectOne(query);

        if (hktsCargoInformation == null) {
            hktsCargoInformation = new HktsCargoInformation();
            hktsCargoInformation.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
            hktsCargoInformation.setDelFlag(0);
            hktsCargoInformation.setCargoType("40");
            hktsCargoInformation.setUpdateTime(LocalDateTime.now());
            hktsCargoInformation.setCreateTime(LocalDateTime.now());
            hktsCargoInformation.setIsEnable("1");
            hktsCargoInformation.setCargoName(cargoName);
            return ResultUtil.data(hktsCargoInformation);
        }else{
            HktsCargoInformation hktsCargoInformations = getbyCargoInformationDataT(hktsCargoInformation);
            return ResultUtil.data(hktsCargoInformations);
        }
    }
}
